var express = require('express');
var router = express.Router();
var mongoose = require('mongoose')
var Goods = require('../module/goods')
const Users = require('./../module/users')

mongoose.connect('mongodb://127.0.0.1:27017/shopmall')

mongoose.connection.on('connected', () => {
    console.log('succcess and ok')
})

mongoose.connection.on('error', () => {
    console.log('error and failure')
})

//商品列表操作
router.get('/', (req, res) => {
    let page = parseInt(req.param('page'))
    let pageSize = parseInt(req.param('pageSize'))
    let sort = parseInt(req.param('sort'))
    let skip = (page - 1) * pageSize
    let priceLevel = parseInt(req.param('priceCheck'))
    var priceGT = ''; var priceLT = '';
    var obj = {}
    if (priceLevel != -1) {
        switch (priceLevel) {
            case 0: priceGT = 0; priceLT = 100; break;
            case 1: priceGT = 100; priceLT = 500; break;
            case 2: priceGT = 500; priceLT = 1000; break;
            case 3: priceGT = 1000; priceLT = 5000; break;
        }
        obj = {
            salePrice: {
                $gt: priceGT,
                $lte: priceLT
            }
        }
    }

    let goodsModel = Goods.find(obj).skip(skip).limit(pageSize)
    goodsModel.sort({ 'salePrice': sort })
    goodsModel.exec((error, data) => {
        if (error) {
            res.json({
                status: 1,
                msg: error.message
            })
        } else {
            res.json({
                status: 0,
                msg: '',
                result: {
                    count: data.length,
                    list: data
                }
            })
        }
    })

})

//加入购物车操作
router.post('/addCar', (req, res) => {
    var userId = '100000077'
    var productId = req.body.productId
    Users.findOne({ userId: userId }, (error1, user) => {
        if (error1) {
            res.json({
                status: 1,
                mes: error1.message
            })
        } else {
            if (user) {
                let gooditem = ''
                user.cartList.forEach(item => {
                    if (item.productId == productId) {
                        gooditem = item
                        gooditem.productNum++
                        console.log('找到啦');
                    }
                })
                if (gooditem) {
                    user.save((error, doc) => {
                        if (error) {
                            console.log('失败失败了')
                        } else {
                            res.json({
                                status: 0,
                                msg: '',
                                result: 'success and ok'
                            })
                        }
                    })

                }

                else {
                    Goods.findOne({ productId: productId }, (error2, data) => {
                        if (error2) {
                            res.json({
                                status: 1,
                                mes: error2.message
                            })
                        } else {
                            user.cartList.push(data)

                            user.save((error3, doc) => {
                                if (error3) {
                                    res.json({
                                        status: 1,
                                        mes: error3.message
                                    })
                                } else {
                                    res.json({
                                        status: 0,
                                        msg: '',
                                        result: 'success and ok'
                                    })
                                }
                            })

                        }
                    })
                }

            }

        }
    })

})

module.exports = router



